package com.tmc.web.controller.inthotel;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.inthotel.hotel.Hotel;
import com.tmc.service.inthotel.impl.TouricoHolidayServiceImpl;
import com.tmc.util.ObjectUtil;
import com.tmc.vendor.inthotel.tourico.service.HotelService;

@Controller
@RequestMapping("/hotel")
public class HotelController {

	private Logger log = Logger.getLogger(TouricoHolidayServiceImpl.class);
	
	@Autowired
	private HotelService service;
	private String cityName;
	
	@RequestMapping(value="/searchHotel", method = RequestMethod.POST)
	@ResponseBody
	public String searchHotel(String query){
		if(ObjectUtil.noText(query)){
			query = "#";
		}
		if(ObjectUtil.noText(this.cityName)){
			this.cityName = "#";
		}
		query = this.cityName+","+query;
		List<Hotel> hotels = service.searchHotel(query);
		JSONArray array = new JSONArray();
		for (Hotel hotel : hotels) {
			JSONObject obj = new JSONObject();
			obj.put("Key", hotel.getId());
			if(hotel.getHotelNameCN()==null){
				obj.put("Name", hotel.getHotelName()+"("+hotel.getAirportCode()+")");
			}else{
				obj.put("Name", hotel.getHotelNameCN()+"("+hotel.getAirportCode()+")");
			}
			array.add(obj);
		}
		return array.toJSONString();
	}
	
	@RequestMapping(value="/searchCityName", method = RequestMethod.POST)
	@ResponseBody
	public String searchCityName(String query){
		this.cityName = query;
		return "true";
	}
	
}
